<template>
	<view>
	<!-- 开启消息提示 -->
	<u-toast ref="uToast" />
	<view class="u-m-t-30 u-m-l-30 u-font-18 u-title">上传附件</view>
	<u-form :model="form" ref="uForm" class="u-m-l-30 u-text-center">
		<u-form-item label="附件" >
		<u-input v-model="form.file_name" />
		<u-button shape="square"  class="u-m-r-20" @click="chooseFile">选择</u-button>
		</u-form-item>
     </u-form>
	 <u-button type="primary"  class="u-m-t-50 u-m-l-20 u-m-r-20" @click="submit">上传附件</u-button>
	 </view>
</template>

<script>
	export default {
		data() {
			
			return {
				form:{
					//文件名
					file_name:''
				},
				//文件临时地址
				temp_file:'',
				//文件在云储存的位置
				temp_url:''
			
			}
		},
		methods: {
			chooseFile(){
				
				console.log("123123")
				let _this=this
				uni.chooseFile({
					count:1,
					success(res) {
						
						// console.log(res.tempFiles[0])
						// console.log(res.tempFiles[0].name)
						//显示附件名
						_this.form.file_name=res.tempFiles[0].name
						//将附件的临时url存储起来
						_this.temp_file=res.tempFiles[0].path
						
						
					}
				})
			},
			//格式化当前日期的写法
			getNowDate(){
				var nowDate = new Date();
				 var year = nowDate.getFullYear();
				 var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1)
				  : nowDate.getMonth() + 1;
				 var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
				  .getDate();
				 var dateStr = year + "-" + month + "-" + day;
				 return dateStr;
			},
			submit(){
				// console.log("123")
				let _this=this
				// console.log(_this.form)
				// console.log(_this.temp_file)
				uniCloud.uploadFile({
					filePath:_this.temp_file,//文件的临时路径
					cloudPath:_this.form.file_name,//在云存储中的名字
					success:function(res){
						//获取其在空间中的id/url
						// console.log("<<<<",res)
						// console.log(res.fileID)
						_this.temp_url=res.fileID
						//表单中替换
						// _this.form.head_image=res.fileID
						//在附件表中添加一条附件信息
						uniCloud.callFunction({
												name: "resume",
												data: {	
													file_name:_this.form.file_name,
													url:_this.temp_url,
													time:_this.getNowDate(),
													user_id:uni.getStorageSync('storage_user')!=''?uni.getStorageSync('storage_user').data[0]._id:'',
													type: "addResume"
												},
												success: (res) => {
													console.log("error！",res);
													// console.log(res.result.data.id)
													if(res.result.data.id===''){
														// console.log("error！",res);
														//提示消息
														_this.$refs.uToast.show({
																title: '附件上传失败！',
																type: 'error',//提示文字
																position:"top",
																duration:'1000',//时间
																icon:false
																})
															return;
													}
													//成功
													//提示消息
													_this.$refs.uToast.show({
															title: '附件上传成功！',
															type: 'success',//提示文字
															position:"top",
															icon:false,
															duration:'1000',//时间
															})	
															
												},
												fail: (err) => {
													console.log(err)
												}
											})
				}
				})
			},
			
		},
		
		onLoad() {
		
		},
		
	}
</script>

<style>
.footbox{
/* 	position: fixed;
	bottom: 10rpx;
	height: 70rpx;
	right: 270rpx; */
}
</style>
